/**
 * 权限管理的JS方法
 */
XF.Setting = new function(){

	/**
	 * 新建用户
	 */
	this.addUser = function(){
		XF.lightSideBar('addUser');
		var url  = global.contextPath + "/setting/addUser"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#settingCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 删除用户
	 */
	this.deleteUser = function(userId){
		$.messager.confirm('提示','确认删除用户：'+userId+"？",function(r){
		    if (r){
				var url  = global.contextPath + "/setting/doDeleteUser"
				$.ajax({
				  url: url,
				  type: "post",
				  data: {userId:userId},
				  complete: function(xhr){
					  xhr = xhr['responseText'];
					  xhr = new Function("return " + xhr);
					  xhr = xhr();
					  xhr = xhr['status'];
					  if(xhr=="SUCCESS"){
						  $.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						  });
						  $(".userListWrap tr i[s="+userId+"]").parent().parent().parent().remove();
					  }else if(xhr=="FAILD"){
						  $.messager.show({
							title:'提示',
							msg:'删除失败',
							timeout:5000,
							showType:'slide'
						  });
					  }
				   }
				});
		    }
		});
	};
	
	this.editUser = function(userId){
		var url  = global.contextPath + "/setting/editUser"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {userId:userId},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#settingCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 用户列表
	 */
	this.listUser = function(){
		XF.lightSideBar('listUser');
		_search_listUser(null,null,null,null,null,null,null,null,null);
	};
	
	this.listUserSearch = function(orderByFields){
		var orderByField = null;
		var orderByMode = null;
		if(typeof(orderByFields)!="undefined"){
			orderByField = orderByFields;
		}
    	var td = $("th[orderByField="+orderByField+"]");
    	var orderByMode = td.find("span.px").attr("orderByMode");
    	
		var userId = $("input[name=userId]").val();
		var userName = $("input[name=userName]").val();
		var siteId = $("input[name=siteId]").val();
		
		var deptId = $("input[name=deptId]").val();
		var enabled = $("select[name=enabled]").val();
		var createDateBegin =  $('#createDateBegin').datebox('getValue');
		var createDateEnd =  $('#createDateEnd').datebox('getValue');
		
		_search_listUser(orderByField,orderByMode,userId,userName,siteId,deptId,enabled,createDateBegin,createDateEnd);
	};
	
	function _search_listUser(orderByField,orderByMode,userId,userName,siteId,deptId,enabled,createDateBegin,createDateEnd){
    	config.listUser_search['args'] = [orderByField,orderByMode,userId,userName,siteId,deptId,enabled,createDateBegin,createDateEnd];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.listUser_search);
	}
	
	
	this.initAddRole = function(){
		var checkbox = $("input.checkboxSelectAll");
		var checkboxs = $("input:checkbox[name=permissioins]");
		checkbox.bind("change",function(e){
			var v = $("input.checkboxSelectAll")[0].checked;
			if(v){
				checkboxs.each(function() {
					$(this)[0].checked = true;
				});
			}else{
				checkboxs.each(function() {
			        $(this)[0].checked = false;
				});
			}
		});
		
		checkboxs.bind("change",function(e){
			var v = $(this)[0].checked;
			if(!v){
				if(checkbox[0].checked){
					checkbox[0].checked = false;
				}
			}else if(v){
				var flag = true;
				for(var i=0;i<checkboxs.length;i++){
					var c = checkboxs[i];
					if(!c.checked){
						flag = false;
					}
				}
				if(flag){
					if(!checkbox[0].checked){
						checkbox[0].checked = true;
					}
				}
			}
		});
		
	}
	
	/**
	 * 新建角色
	 */
	this.addRole = function(){
		XF.lightSideBar('addRole');
		var url  = global.contextPath + "/setting/addRole"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#settingCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 删除角色
	 */
	this.deleteRole = function(roleId,roleDesc){
		$.messager.confirm('提示','确认删除角色：'+roleDesc+"？",function(r){
		    if (r){
				var url  = global.contextPath + "/setting/doDeleteRole"
				$.ajax({
				  url: url,
				  type: "post",
				  data: {roleId:roleId},
				  complete: function(xhr){
					  xhr = xhr['responseText'];
					  xhr = new Function("return " + xhr);
					  xhr = xhr();
					  xhr = xhr['status'];
					  if(xhr=="SUCCESS"){
						  $.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						  });
						  $(".userListWrap tr i[s="+roleId+"]").parent().parent().parent().remove();
					  }else if(xhr=="FAILD"){
						  $.messager.show({
							title:'提示',
							msg:'删除失败',
							timeout:5000,
							showType:'slide'
						  });
					  }
				   }
				});
		    }
		});
	};
	
	
	/**
	 * 编辑角色
	 */
	this.editRole = function(roleId){
		var url  = global.contextPath + "/setting/editRole"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {roleId:roleId},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#settingCenter").html(xhr);
		  }
		});
	};
	
	
	this.listRole = function(){
		XF.lightSideBar('listRole');
		_search_listRole(null,null,null);
	};
	
	this.listRoleSearch = function(orderByFields){
		var orderByField = null;
		var orderByMode = null;
		if(typeof(orderByFields)!="undefined"){
			orderByField = orderByFields;
		}
    	var td = $("th[orderByField="+orderByField+"]");
    	var orderByMode = td.find("span.px").attr("orderByMode");
		var roleDesc = $("input[name=roleDesc]").val();
		
		_search_listRole(orderByField,orderByMode,roleDesc);
	};
	
	function _search_listRole(orderByField,orderByMode,roleDesc){
    	config.listRole_search['args'] = [orderByField,orderByMode,roleDesc];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.listRole_search);
	}
	
	this.addDevice = function(){
		XF.lightSideBar('addDevice');
		var url  = global.contextPath + "/setting/addDevice"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#settingCenter").html(xhr);
		  }
		});
	};
	
	this.listDevice = function(){
		XF.lightSideBar('listDevice');
		_search_listDevice(null);
	};
	
	this.listDeviceSearch = function(){
		var sn = $("input[name=sn]").val();
		var status = $("select[name=status]").val();
		_search_listDevice(sn,status);
	};
	
	function _search_listDevice(sn,status){
    	config.device_search['args'] = [sn,status];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.device_search);
	}
	
	this.deleteDevice = function(id) {
		$.messager.confirm('操作提示','确定删除这条记录吗',function(data){
			if(data){
				$.ajax({
					url:""+global.contextPath+"/setting/deleteDevice",
					type:"post",
					data:"id="+id,
					success:function(data){
						$.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						});
						XF.Setting.listDeviceSearch();
					}
				});
			}
		});		
	};
	
}();

/**
 * 用户列表请求体
 */
config.listUser_search = {
	url: ""+global.contextPath+"/setting/listUser/",
    argsOrder: ['orderByField','orderByMode','userId','userName','siteId','deptId','enabled','createDateBegin','createDateEnd'],
    needVar: true,
    replaceExp: '#settingCenter',
    callBack: function() {
    }		
};

/**
 * 角色列表请求体
 */
config.listRole_search = {
	url: ""+global.contextPath+"/setting/listRole/",
    argsOrder: ['orderByField','orderByMode','roleDesc'],
    needVar: true,
    replaceExp: '#settingCenter',
    callBack: function() {
    }		
};

config.device_search = {
		url: ""+global.contextPath+"/setting/deviceList/",
	    argsOrder: ['sn','status'],
	    needVar: true,
	    replaceExp: '#settingCenter',
	    callBack: function() {
	    }		
	};